package com.ruoyi.mall.controller;

import java.util.List;
import java.util.Arrays;
import cn.dev33.satoken.annotation.SaIgnore;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.mall.domain.vo.MallGoodsCategoryVo;
import com.ruoyi.mall.domain.bo.MallGoodsCategoryBo;
import com.ruoyi.mall.service.IMallGoodsCategoryService;

/**
 * 商城商品分类信息
 *
 * @author ruoyi
 * @date 2024-04-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/mall/goodsCategory")
public class MallGoodsCategoryController extends BaseController {

    private final IMallGoodsCategoryService iMallGoodsCategoryService;

    /**
     * 查询商城商品分类信息列表
     */
    @SaCheckPermission("mall:goodsCategory:list")
    @GetMapping("/list")
    public R<List<MallGoodsCategoryVo>> list(MallGoodsCategoryBo bo) {
        return R.ok(iMallGoodsCategoryService.queryList(bo));
    }

    /**
     * 导出商城商品分类信息列表
     */
    @SaCheckPermission("mall:goodsCategory:export")
    @Log(title = "商城商品分类信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(MallGoodsCategoryBo bo, HttpServletResponse response) {
        List<MallGoodsCategoryVo> list = iMallGoodsCategoryService.queryList(bo);
        ExcelUtil.exportExcel(list, "商城商品分类信息", MallGoodsCategoryVo.class, response);
    }

    /**
     * 获取商城商品分类信息详细信息
     *
     * @param categoryId 主键
     */
    @SaCheckPermission("mall:goodsCategory:query")
    @GetMapping("/{categoryId}")
    public R<MallGoodsCategoryVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long categoryId) {
        return R.ok(iMallGoodsCategoryService.queryById(categoryId));
    }

    /**
     * 新增商城商品分类信息
     */
    @SaCheckPermission("mall:goodsCategory:add")
    @Log(title = "商城商品分类信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody MallGoodsCategoryBo bo) {
        return toAjax(iMallGoodsCategoryService.insertByBo(bo));
    }

    /**
     * 修改商城商品分类信息
     */
    @SaCheckPermission("mall:goodsCategory:edit")
    @Log(title = "商城商品分类信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody MallGoodsCategoryBo bo) {
        return toAjax(iMallGoodsCategoryService.updateByBo(bo));
    }

    /**
     * 删除商城商品分类信息
     *
     * @param categoryIds 主键串
     */
    @SaCheckPermission("mall:goodsCategory:remove")
    @Log(title = "商城商品分类信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{categoryIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] categoryIds) {
        return toAjax(iMallGoodsCategoryService.deleteWithValidByIds(Arrays.asList(categoryIds), true));
    }

    /**
     * 获取所有商品分类
     * @return
     */
    @SaIgnore
    @RequestMapping("/getGoodsCategoryList")
    public R<List<MallGoodsCategoryVo>> getGoodsList()
    {
        return R.ok(iMallGoodsCategoryService.getGoodsList());
    }
}
